package com.test.nnooolwdeaer.activity_fragment_wooden.info_wooden;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.ImageButton;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseInfoActivity_wooden;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.OcrInfo_Viet_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.constant_wooden.Url_wooden;
import com.test.nnooolwdeaer.dialog_wooden.PhotoUploadDialog;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.UriUtil;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.util_wooden.imageUtil_wooden.ImageConvert_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.AddressPickView_Viet_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.DatePickeView_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.InputView_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.SelectView_wooden;
import com.test.nnooolwdeaer.view_wooden.widget_wooden.TitleView_wooden;

import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

public class OcrUploadActivity_Viet extends BaseInfoActivity_wooden {

    private ImageButton ocrImageIb;

    private InputView_wooden idNumInv;
    private InputView_wooden nameInv;
    private DatePickeView_wooden birthdayDpv;
    private SelectView_wooden genderSv;
    private AddressPickView_Viet_wooden addressApv;
    private InputView_wooden addressDetailInv;
    private DatePickeView_wooden expireDateDpv;

    private String imageUrl;
    private Bitmap imageBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ocr_upload_viet);
        init();
    }

    private void init(){
        TitleView_wooden titleView = findViewById(R.id.activity_ocr_upload_view_title);
        setBackBtn_wooden(titleView.getBackIb_wooden());

        ocrImageIb = findViewById(R.id.activity_ocr_upload_ib_ocr_image);
        idNumInv = findViewById(R.id.activity_ocr_upload_inv_id_num);
        nameInv = findViewById(R.id.activity_ocr_upload_inv_name);
        birthdayDpv = findViewById(R.id.activity_ocr_upload_dpv_birthday);
        genderSv = findViewById(R.id.activity_ocr_upload_sv_gender);
        addressApv = findViewById(R.id.activity_ocr_upload_apv_address);
        addressDetailInv = findViewById(R.id.activity_ocr_upload_inv_address_detail);
        expireDateDpv = findViewById(R.id.activity_ocr_upload_dpv_expire_date);

        genderSv.addListItem_wooden("Nam giới","Nữ giới");

        ocrImageIb.setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("OCR_UPLOAD_ACTIVITY_IMAGE_UPLOAD_CLICK");
            new PhotoUploadDialog(this).show();
        });

        findViewById(R.id.activity_ocr_upload_btn_submit).setOnClickListener(v -> {
            HttpManager_wooden.getInstance_wooden().saveUserBuriedPointAndUpload("OCR_UPLOAD_ACTIVITY_NEXT_BTN_CLICK");
            if (TextUtils.isEmpty(imageUrl)){
                showToast_wooden("Tải lên chứng minh nhân dân của bạn");
                return;
            }
            if (idNumInv.isInputEmpty()|| nameInv.isInputEmpty()||birthdayDpv.isInputEmpty()||genderSv.isInputEmpty()
                    ||addressApv.isInputEmpty()||addressDetailInv.isInputEmpty()||expireDateDpv.isInputEmpty()){
                showToast_wooden(getString(R.string.word_input_tip));
                return;
            }
            HttpManager_wooden.getInstance_wooden().uploadInfo_wooden(Url_wooden.PATH_BVN_INFO,new TreeMap<String,String>(){{
                put("imageUrl",imageUrl);
                put("idcard_number",idNumInv.getInput_wooden());
                put("name",nameInv.getInput_wooden());
                put("birthday",birthdayDpv.getInput_wooden());
                put("gender",genderSv.getInput_wooden());
                put("hometown",addressApv.getInput_wooden());
                put("address",addressDetailInv.getInput_wooden());
                put("expire_date",expireDateDpv.getInput_wooden());
            }});
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PhotoUploadDialog.Start_Camera -> onPhotoGet();
            case PhotoUploadDialog.Open_Album -> {
                if (data == null) return;
                photoUri = data.getData();
                onPhotoGet();
            }
        }
    }

    private void onPhotoGet() {
        if (photoUri == null) {
            imageUrl = null;
            showToast_wooden(getString(R.string.photo_upload_tip));
            return;
        }
        try {
            imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);
            String covertPath = UriUtil.getFilePathFromUri(this, photoUri);
            if (covertPath != null) {
                ImageConvert_wooden.startConvertImage_wooden(
                        new File(covertPath),
                        (localPath, thumpImg) -> ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(
                                () -> HttpManager_wooden.getInstance_wooden().uploadImage(localPath)
                        )
                );
            } else {
                imageUrl = null;
                showToast_wooden(getString(R.string.photo_upload_tip));
            }
        } catch (IOException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    public void onPhotoUploadResponse(String imageUrl){
        if (TextUtils.isEmpty(imageUrl)||imageBitmap==null){
            showToast_wooden(getString(R.string.photo_upload_tip));
            return;
        }
        this.imageUrl = imageUrl;
        ocrImageIb.setBackground(null);
        ocrImageIb.setImageBitmap(imageBitmap);

        HttpManager_wooden.getInstance_wooden().getDiscernIcrInfo(imageUrl);
    }

    public void onGetDiscernIcrInfoResponse(){
        idNumInv.setInput_wooden(OcrInfo_Viet_wooden.getInstance_wooden().getIdcard_number());
        nameInv.setInput_wooden(OcrInfo_Viet_wooden.getInstance_wooden().getName());
        birthdayDpv.setInput(OcrInfo_Viet_wooden.getInstance_wooden().getBirthday());
        genderSv.setInput_wooden(OcrInfo_Viet_wooden.getInstance_wooden().getGender());
        addressApv.setInput(OcrInfo_Viet_wooden.getInstance_wooden().getHometown());
        addressDetailInv.setInput_wooden(OcrInfo_Viet_wooden.getInstance_wooden().getAddress());
        expireDateDpv.setInput(OcrInfo_Viet_wooden.getInstance_wooden().getExpire_date());
    }
}